{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Interactive Chart with Cross-Highlight\n",
    "======================================\n",
    "This example shows an interactive chart where selections in one portion of\n",
    "the chart affect what is shown in other panels. Click on the bar chart to\n",
    "see a detail of the distribution in the upper panel.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# category: interactive\n",
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from vega_datasets import data\n",
    "\n",
    "pts = alt.selection(type=\"single\", encodings=['x'])\n",
    "\n",
    "rect = alt.Chart(data.movies.url).mark_rect().encode(\n",
    "    alt.X('IMDB_Rating:Q', bin=True),\n",
    "    alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),\n",
    "    alt.Color('count()',\n",
    "        scale=alt.Scale(scheme='greenblue'),\n",
    "        legend=alt.Legend(title='Total Records')\n",
    "    )\n",
    ")\n",
    "\n",
    "circ = rect.mark_point().encode(\n",
    "    alt.ColorValue('grey'),\n",
    "    alt.Size('count()',\n",
    "        legend=alt.Legend(title='Records in Selection')\n",
    "    )\n",
    ").transform_filter(\n",
    "    pts.ref()\n",
    ")\n",
    "\n",
    "bar = alt.Chart(data.movies.url).mark_bar().encode(\n",
    "    x='Major_Genre:N',\n",
    "    y='count()',\n",
    "    color=alt.condition(pts, alt.ColorValue(\"steelblue\"), alt.ColorValue(\"grey\"))\n",
    ").properties(\n",
    "    selection=pts,\n",
    "    width=550,\n",
    "    height=200\n",
    ")\n",
    "\n",
    "alt.vconcat(\n",
    "    rect + circ,\n",
    "    bar\n",
    ").resolve_legend(\n",
    "    color=\"independent\",\n",
    "    size=\"independent\"\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
